package com.sunnuwell.demo.controller;

import com.alibaba.fastjson.JSON;
import com.sunnuwell.demo.annotation.UserLoginToken;
import com.sunnuwell.demo.common.exception.AdException;
import com.sunnuwell.demo.entity.Role;
import com.sunnuwell.demo.service.RoleService;
import com.sunnuwell.demo.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: entry
 * @description: 角色管理controller
 * @author: Mr.lv
 * @create: 2019-04-28 09:33
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1")
public class RoleController {

    private RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * @Description: 获取所有的角色
     * @Param: []
     * @return: com.sunnuwell.demo.entity.Role
     * @Author: Mr.Lv
     * @Date: 2019/4/28
     */
    @GetMapping("getAllRole")
    @UserLoginToken
    public List<Role> getAllRole() {
        try {
            List<Role> roleList = roleService.getAllRole();
            return roleList;
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }

    /**
     * @Description: 新增/更新角色
     * @Param: [role]
     * @return: com.sunnuwell.demo.common.vo.CommonResponse
     * @Author: Mr.Lv
     * @Date: 2019/4/28
     */
    @PostMapping("/saveRole")
    @UserLoginToken
    public String saveRole(@RequestBody RoleVo roleVo) throws AdException {
        log.info("RoleController: add/update -> {}",
                JSON.toJSONString(roleVo));
        return roleService.saveRole(roleVo);

    }

    /**
     * @Description: 删除角色
     * @Param: [roleVo]
     * @return: com.sunnuwell.demo.common.vo.CommonResponse
     * @Author: Mr.Lv
     * @Date: 2019/4/28
     */
    @DeleteMapping("/deleteRole")
    public String deleteResource(@RequestBody RoleVo roleVo) throws AdException {
        log.info("RoleController: delete_id -> {}",
                roleVo.getRid());
        return roleService.deleteRole(roleVo);

    }
}